package com.glsc.ngateway.common.base.repo.mysql.gateway.ldap;

import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.LdapEhrEmp;
import com.glsc.ngateway.common.api.ldap.dto.LdapEhrEmpSearchDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lium
 * @date 2021/12/21
 */
public interface LdapEhrEmpRepo extends JpaRepository<LdapEhrEmp, String>, JpaSpecificationExecutor<LdapEhrEmp> {

    LdapEhrEmp findByBadge(String userId);

    List<LdapEhrEmp> findByTitle(String title);

    List<LdapEhrEmp> findByName(String name);

    List<LdapEhrEmp> findAllByBadgeIn(List<String> badge);

    @Transactional
    @Modifying
    @Query(value = "truncate table t_ldap_ehr_emp", nativeQuery = true)
    Integer truncateTableEhrEmp();

    @Query(value = "select m.badge from t_ldap_ehr_emp m ", nativeQuery = true)
    List<String> findExistIdList();

    List<LdapEhrEmp> findByDepid(Integer depid);

    List<LdapEhrEmp> findByDepidAndJobId(Integer depid, Integer jobId);

    List<LdapEhrEmp> findAllByReportto(String userAccount);

    List<LdapEhrEmp> findAllByReporttoIn(List<String> userAccountList);

    List<LdapEhrEmp> findAllByTitleIn(List<String> depts);

    @Query(value = "select * from t_ldap_ehr_emp t " +
            "where 1=1 " +
            "and (coalesce(:#{#param.badgeList}, null) is null or t.badge in (:#{#param.badgeList})) " +
            "and (:#{#param.badge} is null or t.badge = :#{#param.badge}) " +
            "and (:#{#param.mobile} is null or t.mobile = :#{#param.mobile}) " +
            "and (:#{#param.title} is null or t.title = :#{#param.title}) " +
            "and (coalesce(:#{#param.oaUserIdList}, null) is null or t.oa_user_id in (:#{#param.oaUserIdList})) " +
            "and (:#{#param.oaUserId} is null or t.oa_user_id = :#{#param.oaUserId}) " +
            "", nativeQuery = true)
    List<LdapEhrEmp> query(@Param("param") LdapEhrEmpSearchDto param);


    List<LdapEhrEmp> findAllByOaUserIdIn(List<Integer> oaUserIdList);
}
